﻿Partial Public Class ReportExplorer
    Dim aParamFrm As MqParamForm
    Dim lastReportName As String = String.Empty

    'Public Sub Init_TreeActionOptionList(sender As Object, e As EventArgs)
    '    Me.TreeActionOptionList.Add(New ActionOption("DocumentTemplate", "ExecuteReport", "Execute", AddressOf ExecuteReport, True, True, 1))
    'End Sub

    Public Property Icon As System.Drawing.Image


    ''' <summary>
    ''' This sub execute a Report by the given name
    ''' </summary>
    Public Overridable Sub ExecuteReport(ByVal sender As Object, ByVal e As EventArgs)
        Me.Cursor = Cursors.WaitCursor

        Dim dataset As DataSet = New DataSet()
        Dim sr As System.IO.StringReader
        Dim mq As MetaQuery
        Dim sqlcmd As System.Data.SqlClient.SqlCommand = Nothing

        Dim aExecQuery As DialogResult = DialogResult.OK


        '  Dim node As TreeListNode = CType(CType(sender, DXMenuItem).Tag, TreeListNode)
        Dim reportName As String = NodeDataRow("Name")

        Dim report As DevExpress.XtraReports.UI.XtraReport = SCIEF.ActiveFramework.CommonExt.RptNotification.GetReportFromDB(reportName)

        If report IsNot Nothing Then
            If report.DataSourceSchema.Length > 0 Then

                sr = New System.IO.StringReader(report.DataSourceSchema)
                dataset.ReadXmlSchema(sr)
                mq = MetaQuery.GetMetaQueryByName(dataset.DataSetName)

                If mq.MetaParams.Rows.Count > 0 Then

                    If Not reportName = lastReportName Then
                        lastReportName = reportName
                        aParamFrm = New MqParamForm(mq.QueryName)
                    End If

                    aExecQuery = aParamFrm.ShowDialog()

                    'On dit OK pour la form de Parametre
                    If aExecQuery = DialogResult.OK Then
                        sqlcmd = mq.InitQueryParams

                        'On prend les parametre défini par la MQ, pour les retrouver dans le sqlCmd.Parameters
                        'Les metaColumns on la priorité
                        Dim param As SqlClient.SqlParameter
                        For Each row As DataRow In mq.MetaParams.Rows
                            If sqlcmd.Parameters.Contains(row("ColumnName").ToString) Then
                                param = sqlcmd.Parameters(row("ColumnName").ToString)
                                If param IsNot Nothing AndAlso Not IsDBNull(aParamFrm.SelectedParam(row("ColumnName").ToString)) Then
                                    param.Value = aParamFrm.SelectedParam(row("ColumnName").ToString)
                                End If
                            Else
                                MessageWindow.ShowMessage(New Exception(String.Format("Report Builder Load, SQL Parameter not found: {0}", row("ColumnName"))))
                            End If
                        Next
                    End If
                    If aExecQuery = DialogResult.OK Then
                        mq.FillResultQuery(sqlCmd:=sqlcmd)


                        dataset = New DataSet()
                        dataset.Tables.Add(mq.ResultQuery)

                        If mq.GetChildDisplayQuery.Rows.Count > 0 Then

                            Dim strListOfParentGUID As String = "'" + Guid.Empty.ToString + "',"
                            For Each aRow As DataRow In mq.ResultQuery.Rows
                                strListOfParentGUID = strListOfParentGUID + "'" + aRow(mq.PKColumn_Name).ToString + "',"
                            Next

                            For Each row As DataRow In mq.GetChildDisplayQuery.Rows
                                Using mq2 As MetaQuery = MetaQuery.GetMetaQueryByName(row("QueryName").ToString)
                                    mq2.SQLWhere = mq2.FKColumn_Name + " in (" + Mid(strListOfParentGUID, 1, Len(strListOfParentGUID) - 1) + ")"
                                    mq2.FillResultQuery()
                                    dataset.Tables.Add(mq2.ResultQuery)

                                    Dim aRelation As New DataRelation(row("Relation").ToString, mq.ResultQuery.Columns(row("PKColumn_Name").ToString), mq2.ResultQuery.Columns(row("FKColumn_Name").ToString))
                                    dataset.Relations.Add(aRelation)
                                    '
                                End Using
                            Next
                        End If
                    End If
                Else
                    mq.FillResultQuery()

                    dataset = New DataSet()
                    dataset.Tables.Add(mq.ResultQuery)

                    For Each row As DataRow In mq.GetChildDisplayQuery.Rows
                        Using mq2 As MetaQuery = MetaQuery.GetMetaQueryByName(row("QueryName").ToString)
                            mq2.FillResultQuery()
                            dataset.Tables.Add(mq2.ResultQuery)

                            Dim aRelation As New DataRelation(row("Relation").ToString, mq.ResultQuery.Columns(row("PKColumn_Name").ToString), mq2.ResultQuery.Columns(row("FKColumn_Name").ToString))
                            dataset.Relations.Add(aRelation)
                            '
                        End Using
                    Next
                End If


                If aExecQuery = DialogResult.OK Then
                    report.DataSource = dataset
                    sr.Dispose()
                End If

            End If
            '
            report.CreateDocument()
            ' this need to be refactored
            reportName = reportName & " " & String.Join("; ", aParamFrm.SelectedParamText)
            '
            Dim aFrm As Form = New SCIEF.ActiveFramework.WinFormExt.ReportPrintForm() With {.Text = reportName, .Report = report, .Icon = ImageExt.ToIcon(Icon, 16, True), .ShowIcon = True}
            afrm.MdiParent = Me.ParentForm


            aFrm.Show()
        Else
            MessageWindow.ShowMessage("Report was not found.")
        End If

        Me.Cursor = Cursors.Default
    End Sub


    Public Sub TreeActionOptionListLoad()
        Me.MetaControlTreeListActionOptionList.Add(New ActionOption("DocumentTemplate", "ExecuteReport", "Execute", AddressOf ExecuteReport, True, True, 1))
    End Sub
End Class
